using UnityEngine;
using UnityEngine.Events;
using EffectTypeValue = System.Int32;
using BgmTypeValue = System.Int32;

namespace Life.ESound.Bridge
{
    public class ESoundBridge : MonoBehaviour
    {
        [SerializeField] private UnityEvent<float> onMainVolumeChange = new UnityEvent<float>();
        public UnityEvent<float> OnMainVolumeChange => onMainVolumeChange;
        
        
        [SerializeField] private UnityEvent<float> onEffectVolumeChange = new UnityEvent<float>();
        public UnityEvent<float> OnEffectVolumeChange => onEffectVolumeChange;
        
        
        [SerializeField] private UnityEvent<float> onBgmVolumeChange = new UnityEvent<float>();
        public UnityEvent<float> OnBgmVolumeChange => onBgmVolumeChange;
        
        
        [SerializeField] private UnityEvent onBgmComplete = new UnityEvent();
        public UnityEvent OnBgmComplete => onBgmComplete;
        
        
        public System.Func<float> MainVolumeGetter;
        public System.Func<float> EffectVolumeGetter;
        public System.Func<float> BgmVolumeGetter;


        public System.Func<bool> BgmIsPlayerGetter;
        public System.Func<bool> BgmIsPauseGetter;
        
        public System.Action StopBgmRef;
        public System.Action PauseBgmRef;
        public System.Action ResumeBgmRef;
        public System.Action<int,System.Action> StopBgmRefIntAction;
        public System.Action<int,System.Action> PauseBgmRefIntAction;
        public System.Action<int,System.Action> ResumeBgmRefIntAction;

        public System.Action<int> PlayEffectMethodRefInt;
        public System.Action<AudioClip> PlayEffectMethodRefClip;
        public System.Action<int,float> PlayEffectMethodRefIntVolume;
        public System.Action<AudioClip,float> PlayEffectMethodRefClipVolume;
        
        public System.Action<int,bool> PlayBgmMethodRefInt;
        public System.Action<AudioClip,bool> PlayBgmMethodRefClip;

        public static event System.Action OnInitialized;
        public static bool IsInitialized { get; private set; }

        private static ESoundBridge _instance;
        private void Awake()
        {
            _instance = this;
        }

        public void Initialized()
        {
            IsInitialized = true;
            OnInitialized?.Invoke();
        }
        
        public static float MainVolume
        {
            get
            {
                if (_instance == null || _instance.MainVolumeGetter == null)
                    return 0;
                return _instance.MainVolumeGetter.Invoke();
            }
            set
            {
                if(_instance!=null)
                    _instance.onMainVolumeChange?.Invoke(value);
            }
        }

        public static float EffectVolume
        {
            get
            {
                if (_instance == null || _instance.EffectVolumeGetter == null)
                    return 0;
                return _instance.EffectVolumeGetter.Invoke();
            }
            set
            {
                if(_instance!=null)
                    _instance.onEffectVolumeChange?.Invoke(value);
            }
        }

        public static float BgmVolume
        {
            get
            {
                if (_instance == null || _instance.BgmVolumeGetter == null)
                    return 0;
                return _instance.BgmVolumeGetter.Invoke();
            }
            set
            {
                if(_instance!=null)
                    _instance.onBgmVolumeChange?.Invoke(value);
            }
        }
        /// <summary>
        /// Bgm 是否处于播放状态
        /// </summary>
        public static bool BgmIsPlaying 
        {
            get
            {
                if (_instance == null || _instance.BgmIsPlayerGetter == null)
                    return false;
                return _instance.BgmIsPlayerGetter.Invoke();
            }
        }
        /// <summary>
        /// Bgm 是否处于暂停状态
        /// </summary>
        public static bool BgmIsPause
        {
            get
            {
                if (_instance == null || _instance.BgmIsPauseGetter == null)
                    return false;
                return _instance.BgmIsPauseGetter.Invoke();
            }
        }
        /// <summary>
        /// 播放一个音效
        /// </summary>
        /// <param name="type"></param>
        public static void PlayEffect(EffectTypeValue type)
        {
            _instance?.PlayEffectMethodRefInt?.Invoke(type);
        }
        /// <summary>
        /// 播放一个音效
        /// </summary>
        /// <param name="clip"></param>
        public static void PlayEffect(AudioClip clip)
        {
            _instance?.PlayEffectMethodRefClip?.Invoke(clip);
        }
        /// <summary>
        /// 播放一个音效
        /// </summary>
        /// <param name="type"></param>
        /// <param name="volumeScale"></param>
        public static void PlayEffect(EffectTypeValue type,float volumeScale)
        {
            _instance?.PlayEffectMethodRefIntVolume?.Invoke(type,volumeScale);
        }
        /// <summary>
        /// 播放一个音效
        /// </summary>
        /// <param name="clip"></param>
        /// <param name="volumeScale"></param>
        public static void PlayEffect(AudioClip clip,float volumeScale)
        {
            _instance?.PlayEffectMethodRefClipVolume?.Invoke(clip,volumeScale);
        }

        /// <summary>
        /// 播放一个Bgm
        /// </summary>
        /// <param name="type"></param>
        /// <param name="loop"></param>
        public static void PlayBgm(BgmTypeValue type, bool loop)
        {
            _instance?.PlayBgmMethodRefInt?.Invoke(type, loop);
        }
        /// <summary>
        /// 播放一个Bgm
        /// </summary>
        /// <param name="clip"></param>
        /// <param name="loop"></param>
        public static void PlayBgm(AudioClip clip, bool loop)
        {
            _instance?.PlayBgmMethodRefClip?.Invoke(clip, loop);
        }
        /// <summary>
        /// 停止播放Bgm
        /// </summary>
        public static void StopBgm()
        {
            _instance?.StopBgmRef?.Invoke();
        }
        /// <summary>
        /// 暂停播放Bgm
        /// </summary>
        public static void PauseBgm()
        {
            _instance?.PauseBgmRef?.Invoke();
        }
        /// <summary>
        /// 恢复播放Bgm（IsPause 时继续播放，!IsPlaying 时重新播放）
        /// </summary>
        public static void ResumeBgm()
        {
            _instance?.ResumeBgmRef?.Invoke();
        }
        /// <summary>
        /// 停止播放Bgm 音量渐变
        /// </summary>
        /// <param name="rampTime"></param>
        /// <param name="callback"></param>
        public static void StopBgm(int rampTime,System.Action callback)
        {
            _instance?.StopBgmRefIntAction?.Invoke(rampTime, callback);
        }
        /// <summary>
        /// 暂停播放Bgm 音量渐变
        /// </summary>
        /// <param name="rampTime"></param>
        /// <param name="callback"></param>
        public static void PauseBgm(int rampTime,System.Action callback)
        {
            _instance?.PauseBgmRefIntAction?.Invoke(rampTime, callback);
        }
        /// <summary>
        /// 恢复播放Bgm 音量渐变
        /// </summary>
        /// <param name="rampTime"></param>
        /// <param name="callback"></param>
        public static void ResumeBgm(int rampTime,System.Action callback)
        {
            _instance?.ResumeBgmRefIntAction?.Invoke(rampTime, callback);
        }
        
        /*
         *
         *public System.Action StopBgmRef;
           public System.Action PauseBgmRef;
           public System.Action ResumeBgmRef;
           public System.Action<int,System.Action> StopBgmRefIntAction;
           public System.Action<int,System.Action> PauseBgmRefIntAction;
           public System.Action<int,System.Action> ResumeBgmRefIntAction;
         * 
         */
    }
}
